package www.qiyue.sm;

import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.qiyue.utils.UUIDGenerator;

import java.util.List;
import java.util.Map;

@RestController
public class smtsuser {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("smuser")
    public void smtestuser(){
        String sqlList = "SELECT org_code from t_s_depart WHERE org_code<>'03'";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlList);
        for (Map<String, Object> map : list) {
            String generate = UUIDGenerator.generate();
            String string = map.get("org_code").toString();
            String instract="INSERT INTO `ais_bpm_user`(`id`, `create_name`, `create_by`, `create_date`, `update_name`, `update_by`, `update_date`, `sys_org_code`, `sys_company_code`, `bpm_name`, `posgt_name`, `post_code`, `person`, `remark`, `company_id`, `person_name`) VALUES ('"+UUIDGenerator.generate()+"', '管', 'admin', '2019-12-09 00:00:00', '管理员', 'admin', '2019-12-10 00:00:00', '03', '03', '变更流程', '公司总经理', '$$zcbglujtfgld', '001339', NULL, '"+string+"', '吴强');";
            jdbcTemplate.update(instract);
            String instract1="INSERT INTO `ais_bpm_user`(`id`, `create_name`, `create_by`, `create_date`, `update_name`, `update_by`, `update_date`, `sys_org_code`, `sys_company_code`, `bpm_name`, `posgt_name`, `post_code`, `person`, `remark`, `company_id`, `person_name`) VALUES ('"+UUIDGenerator.generate()+"', '管', 'admin', '2019-12-09 00:00:00', '管理员', 'admin', '2019-12-10 00:00:00', '03', '03', '变更流程', '业务负责人', '$$zcbgluywfzr', '000018', NULL, '"+string+"', '沈超');";
            jdbcTemplate.update(instract1);
            String instract2="INSERT INTO `ais_bpm_user`(`id`, `create_name`, `create_by`, `create_date`, `update_name`, `update_by`, `update_date`, `sys_org_code`, `sys_company_code`, `bpm_name`, `posgt_name`, `post_code`, `person`, `remark`, `company_id`, `person_name`) VALUES ('"+generate+"', '管', 'admin', '2019-12-09 00:00:00', '管理员', 'admin', '2019-12-10 00:00:00', '03', '03', '变更流程', '企业分管领导', '$$zcbglujyfgld', '000056', NULL, '"+string+"', '薛莹');";
            jdbcTemplate.update(instract2);
        }

        /*String sqlList = "SELECT * from testrent";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlList);
        for (Map<String, Object> map : list) {
            String rentcode = map.get("rentcode").toString();
            String codename = map.get("codename").toString();
            String pinYinHeadChar = "$$xbp"+getPinYinHeadChar(rentcode);
            String username = "SELECT username from t_s_base_user WHERE realname='"+codename+"'";
            Map<String, Object> forMap = jdbcTemplate.queryForMap(username);
            String user = forMap.get("username").toString();
            String orgCode ="SELECT org_code from t_s_depart WHERE id<>'8a8ab0b246dc81120146dc8180ba0017'";
            List<Map<String, Object>> forList = jdbcTemplate.queryForList(orgCode);
            System.out.println(forList.size());
            for (Map<String, Object> smap : forList) {
                String orgcode = smap.get("org_code").toString();
                String instrSql="INSERT INTO `sm`.`ais_bpm_user`(`id`, `create_name`, `create_by`, `create_date`, `update_name`, `update_by`, `update_date`, `sys_org_code`, `sys_company_code`, `bpm_name`, `posgt_name`, `post_code`, `person`, `remark`, `company_id`) VALUES ('"+ UUIDGenerator.generate()+"', '管理员', 'admin', '2017-08-09 00:00:00', '商贸管理员', 'system', '2020-07-07 00:00:00', '02', '02', '新合同报批流程', '"+rentcode+"', '"+pinYinHeadChar+"', '"+user+"', NULL, '"+orgcode+"')";
                jdbcTemplate.update(instrSql);
            }

        }*/
        /*String sqlList = "SELECT * from testrent";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlList);
        for (Map<String, Object> map : list) {
            String rentcode = map.get("rentcode").toString();
            String codename = map.get("codename").toString();
            String update = "UPDATE ais_bpm_user set person=(SELECT username from t_s_base_user WHERE realname='"+codename+"') WHERE bpm_name='新合同报批流程' and posgt_name='结算平台财务科' and company_id ='"+rentcode+"'";
            jdbcTemplate.update(update);
        }*/
    }


        /**
         * 提取每个汉字的首字母
         *
         * @param str
         * @return String
         */
        private  String getPinYinHeadChar(String str) {
            String convert = "";
            for (int j = 0; j < str.length(); j++) {
                char word = str.charAt(j);
                // 提取汉字的首字母
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
                if (pinyinArray != null) {
                    convert += pinyinArray[0].charAt(0);
                } else {
                    convert += word;
                }
            }
            return convert;
        }

        public void test(){
            String sqlList = "SELECT * from testrent";
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlList);
            for (Map<String, Object> map : list) {
                String rentcode = map.get("rentcode").toString();
                String codename = map.get("codename").toString();
                String update = "UPDATE ais_bpm_user set person=(SELECT username from t_s_base_user WHERE realname='"+codename+"') WHERE bpm_name='新合同报批流程' and posgt_name='结算平台财务科' and company_id ='"+rentcode+"'";
                jdbcTemplate.update(update);
            }
        }


}
